Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
fn instanceof Function VS Object.prototype.toString.call(fn) === "[object Function]"

I see a lot of pros javascript developer testing if an object is a function using

Object.prototype.toString.call(fn) === "[object Function]"

Usually I am using

fn instanceof Function === true

can someone enlighten me. Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

instanceof just looks for the constructor property in the prototype chain. See the standard:

@@hasInstance: A method that determines if a constructor object recognizes an object as one of the constructor's instances. Called by the semantics of the instanceof operator.

Which means you can "trick" it:

let Constructor = function(){};
Constructor.prototype = Function.prototype;

let Not_A_Real_Function = function(){};
Not_A_Real_Function.prototype = new Constructor;

let not_a_real_function = new Not_A_Real_Function;

// instanceof
console.log(not_a_real_function instanceof Function); 
// prints true

// Object.prototype.toString
console.log(Object.prototype.toString.call(not_a_real_function) === "[object Function]");
// prints false

// typeof
console.log(typeof not_a_real_function === "function");
// prints false

A downside of Object.prototype.toString however is that you can potentially override it all together, to make it return whatever you want.

let Object = {
    prototype: {
        toString(fn){
            return "foo";
        }
    }
};

let fc = function(){};
console.log(Object.prototype.toString.call(fc) === "foo");


For the sake of completeness:
As @Bergi pointed out,

typeof fn == "function"

is probably the safest way. (See the first snippet)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!